<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\DB;

class LoginMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(! session('openid')) {
            return redirect('/test');
        }
        $userInfo = DB::table('user')->where('openid', '=', session('openid'));
        if (! $userInfo) {
            return redirect('/test');
        }
        return $next($request);
    }
}
